var pageSize = 20;
var pageStart = 1;
$(function(){
	loadInfo();
	//删除按钮事件
	$("tbody").on('click','.del',function(){
		var id = $(this).attr("name");
		bootbox.confirm("您确认执行该操作？",function(result){
			if(result){
				delTag(id);
			}
		});
	});
});

function delTag(id){
	$.ajax({
		url:BASE_PATH+'/tag/del.do',
		type:'get',
		dataType:'json',
		data:{
			id:id
		},
		success:function(data){
			if(data.status=='success'){
				$("a[name='"+id+"']").parents('tr').remove();
			}else{
				if(data.errorMsg=='expire'){
					window.location.href="login.html"
				}else{
					alert(data.erroeMsg);
				}
			}
		}
	});
}

function loadInfo(){
	$.ajax({
		url:BASE_PATH+'/tag/list.do',
		type:'get',
		dataType : 'json',
		data:{pageSize:pageSize,pageStart:pageStart},
		success : function(data) {
			if(data.errorMsg == 'expire'){
				window.location.href="login.html";
			}else{
				createRows(data);
			}
		}
	});
}

function createRows(data){
	var html = "";
	if(data){
		for(var i = 0; i < data.length; i++){
			html+="<tr>"
				+	"<td>"+data[i].tagName+"</td>"
				+	"<td><span class='label' style='background-color:"+data[i].tagColor+"!important'>"+data[i].tagName+"</span></td>"
				 +"<td>"
				 +"<div class=\"action-buttons\">"
				+		"<a class=\"green\" href=\"addtag.html?id="+data[i].id+"\">"
				+			"<i class=\"icon-pencil bigger-130\"></i>"
				+		"</a>"
				+		"<a class=\"red del\" href=\"#\" name=\""+data[i].id+"\">"
				+			"<i class=\"icon-trash bigger-130\"></i>"
				+		"</a>"
				+	"</div>"
				 +"</td>"
				+"</tr>";
		}
	}
	$("tbody").html(html);
}